﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.IO;
using System.Windows.Media;


namespace PacketBuilder
{    
    public partial class MainWindow : Window
    {
        static public string lastSelectedProtocol = string.Empty;

        // Map with options
        static public Dictionary<string, string> optionsMap = new Dictionary<string, string>();

        // Map with options maps
        static public Dictionary<string, Dictionary<string, string>> optionsMapsMap = new Dictionary<string, Dictionary<string, string>>();

        // Generate option string
        static public string GetOptionsString()
        {
            StringBuilder optionStrBuilder = new StringBuilder();            
            optionStrBuilder.AppendFormat(" --{0}", lastSelectedProtocol);
            foreach (var option in optionsMap)
            {
                optionStrBuilder.AppendFormat(" --{0} {1}", option.Key, option.Value);
            }            
            return optionStrBuilder.ToString();
        }

        public static StackPanel optionsStackPanel = new StackPanel();

        public MainWindow()
        {
            this.Title = stringMainWindowTitle;
            Width = mainWindowWidth;
            Height = mainWindowHeight;
            this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            Background = new SolidColorBrush(Colors.LightGray);
            this.ResizeMode = System.Windows.ResizeMode.CanMinimize;

            Content = GetMainWindowContent();            
        }

        public static UIElement GetMainWindowContent()
        {
            var mainStackPanel = new StackPanel();
            //Add header
            mainStackPanel.Children.Add(new HeaderOptions());

            // Add body           
            optionsStackPanel.Orientation = Orientation.Vertical;
            optionsStackPanel.Height = optionStackPanelHeight;            
            
            ScrollViewer optionsScrollView = new ScrollViewer();            
            optionsScrollView.Content = optionsStackPanel;
            optionsScrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            // Add footer
            mainStackPanel.Children.Add(optionsScrollView);
            mainStackPanel.Children.Add(new ConsolePanel());            

            return mainStackPanel;
        }

        public static string RunCmd(string procName, string args)
        {
            string returnvalue = string.Empty;

            ProcessStartInfo info = new ProcessStartInfo(procName);
            info.Arguments = args;
            info.UseShellExecute = false;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;

            using (Process process = Process.Start(info))
            {                
                StreamReader sr = process.StandardOutput;
                process.WaitForExit();                
                returnvalue = sr.ReadToEnd();
            }            

            return returnvalue;
        }

        [STAThread]
        public static void Main()
        {
            Application app = new Application();

            app.Run(new MainWindow());
        }
    }
}